{% load static get_attribute %}

<tr id="item-{{ item.pk }}" class="hover:bg-gray-100 dark:hover:bg-gray-700 group relative">
    <td class="w-4 p-4">
      <div class="flex items-center">
        <input id="checkbox-194556" aria-describedby="checkbox-1" type="checkbox"
          class="w-4 h-4 border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 dark:focus:ring-primary-600 dark:ring-offset-gray-800 dark:bg-gray-700 dark:border-gray-600">
        <label for="checkbox-194556" class="sr-only">checkbox</label>
      </div>
    </td>
    {% for field_name in db_field_names %}
      <td class="td_{{ field_name }} p-4 text-base font-medium text-gray-900 whitespace-nowrap dark:text-white group-hover:opacity-10">
        {{ item|getattribute:field_name }}
      </td>
    {% endfor %}
  
    {% if request.user.is_authenticated %}
      <td class="p-4 space-x-2 whitespace-nowrap absolute inset-x-2/4 bottom-[-8px] hidden group-hover:block">
        <button type="button"
          data-modal-target="edit-item-{{item.pk}}" 
          data-modal-toggle="edit-item-{{item.pk}}"
          class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white rounded-lg bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-primary-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-primary-800"
        >
            <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                <path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z"></path>
                <path fill-rule="evenodd"
                d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"
                clip-rule="evenodd"></path>
            </svg>
        </button>
        <button type="button"
          data-modal-target="delete-item-{{item.pk}}" data-modal-toggle="delete-item-{{item.pk}}"
          class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-red-600 rounded-lg hover:bg-red-800 focus:ring-4 focus:ring-red-300 dark:focus:ring-red-900">
          <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20"
            xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"
              d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
              clip-rule="evenodd"></path>
          </svg>
        </button>
      </td>
    {% else %}
    <td class="p-4 space-x-2 whitespace-nowrap absolute inset-x-2/4 bottom-[-8px] hidden group-hover:block">
      <div class="flex items-center gap-3">
        <button type="button"
          data-modal-target="view-item-{{item.pk}}" data-modal-toggle="view-item-{{item.pk}}"
          class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white rounded-lg bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-primary-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-primary-800">
          <svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
            <path stroke="currentColor" stroke-width="2" d="M21 12c0 1.2-4.03 6-9 6s-9-4.8-9-6c0-1.2 4.03-6 9-6s9 4.8 9 6Z"/>
            <path stroke="currentColor" stroke-width="2" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"/>
          </svg> 
        </button>

      </div>
    </td>
    {% endif %}
  
</tr>


<script>
  var checkboxes = document.querySelectorAll('#dropdownDefaultCheckbox input[type="checkbox"]');
  
  checkboxes.forEach(function (checkbox) {

    var targetColumnId = checkbox.getAttribute('data-target');
    var targetColumn = document.getElementById('th_' + targetColumnId);
    var exportTargetColumn = document.getElementById('th_' + targetColumnId + '_export');
    var targetDataCells = document.querySelectorAll('.td_' + targetColumnId);

    if (checkbox.checked) {
      targetColumn.style.display = 'none';
      exportTargetColumn.style.display = 'none';
      targetDataCells.forEach(function (dataCell) {
        dataCell.style.display = 'none';
      });
    }

    checkbox.addEventListener('change', function () {
      var targetColumnId = this.getAttribute('data-target');
      var targetColumn = document.getElementById('th_' + targetColumnId);
      var exportTargetColumn = document.getElementById('th_' + targetColumnId + '_export');
      var targetDataCells = document.querySelectorAll('.td_' + targetColumnId);
      
      if (this.checked) {
        targetColumn.style.display = 'none';
        exportTargetColumn.style.display = 'none';
        targetDataCells.forEach(function (dataCell) {
          dataCell.style.display = 'none';
        });
      } else {
        targetColumn.style.display = '';
        exportTargetColumn.style.display = '';
        targetDataCells.forEach(function (dataCell) {
          dataCell.style.display = '';
        });
      }

      fetch(`/create-hide-show-items/${link}/`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'X-CSRFToken': '{{ csrf_token }}',
        },
        body: JSON.stringify({
          key: targetColumnId,
          value: this.checked
        })
      })

    });
  });
</script>